<?php
// +----------------------------------------------------------------------
// | B5Yii2CMF V3.0 [快捷通用基础管理开发平台]
// +----------------------------------------------------------------------
// | Author: 冰舞 <357145480@qq.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace common\services\system;

use common\models\system\AdminRole;
use common\models\system\Menu;
use common\models\system\Role;
use common\models\system\RoleMenu;

class MenuService
{
    /**
     * 菜单类型
     * @return array
     */
    public static function typeList(): array
    {
        return ['M' => '目录', 'C' => '菜单', 'F' => '按钮'];
    }

    /**
     * 获取所有菜单
     * @return array
     */
    public static function getList(): array
    {
        $list = Menu::find()->select(['id', 'parent_id', 'name', 'status'])->orderBy('parent_id asc,list_sort asc , id asc')->asArray()->all();
        return $list ?: [];
    }

    /**
     * 前端展示菜单，非超管
     * @param int $userId
     * @param string $scene 场景  menu 前端右侧菜单
     * @param array $params
     * @return array
     */
    public static function selectUserWebMenuList(int $userId, string $scene = '', array $params = []): array
    {
        if ($userId < 1) return [];
        $where = ['ar.admin_id' => $userId, 'r.status' => '1'];
        $fields = 'm.*';
        if ($scene === 'menu') {
            $where['m.status'] = '1';
            $where['m.type'] = ['M', 'C'];
            $fields = ['m.id', 'm.parent_id', 'm.type', 'm.name', 'm.icon', 'm.perms', 'm.url', 'm.query', 'm.component', 'm.is_external', 'm.is_cache', 'm.is_visible'];
        }
        $query = Menu::find()->alias('m')->select($fields)
            ->leftJoin(RoleMenu::tableName() . ' rm', 'm.id = rm.menu_id')
            ->leftJoin(AdminRole::tableName() . ' ar', 'rm.role_id = ar.role_id')
            ->leftJoin(Role::tableName() . ' r', 'ar.role_id = r.id')
            ->where($where);

        $list = self::_selectWebMenuWhereJoin($query, $params, 'm.')->orderBy('m.parent_id asc,m.list_sort asc')->asArray()->all();
        return $list ?: [];
    }

    /**
     * 前端展示菜单M、C，超管
     * @param string $scene 场景  menu 前端右侧菜单
     * @param array $params
     * @return array
     */
    public static function selectAdminWebMenuList(string $scene = '', array $params = []): array
    {
        $where = [];
        $fields = '*';
        if ($scene === 'menu') {
            $where['status'] = '1';
            $where['type'] = ['M', 'C'];
            $fields = ['id', 'parent_id', 'type', 'name', 'icon', 'perms', 'url', 'query', 'component', 'is_external', 'is_cache', 'is_visible'];
        }
        $query = Menu::find()->select($fields)->where($where);
        $list = self::_selectWebMenuWhereJoin($query, $params)->orderBy('parent_id asc,list_sort asc')->asArray()->all();
        return $list ?: [];
    }

    private static function _selectWebMenuWhereJoin($query, $params, $alias = '')
    {
        if (!$params) return $query;
        $status = $params['status'] ?? '';
        if ($status || $status == '0') $query = $query->andWhere([$alias . 'status' => $status]);
        $name = $params['name'] ?? '';
        if ($name) $query = $query->andWhere(['like', $alias . 'name', $name]);
        return $query;
    }


    /**
     * 查询权限标识信息，用于权限判断
     * @param string $perms
     * @return array
     */
    public static function selectInfoByPerms(string $perms): array
    {
        if (!$perms) return [];
        $info = Menu::findOne(['perms' => $perms]);
        return $info ? $info->toArray() : [];
    }

    /**
     * 根据id获取权限标识
     * @param array $ids
     * @return array
     */
    public static function selectPermsListByMenuId(array $ids): array
    {
        if (!$ids) return [];
        $list = Menu::find()->select(['perms'])->where(['id' => $ids])->column();
        return $list ? array_values(array_unique($list)) : [];
    }
}